const BaseRest = require('./rest.js');
const { think } = require('thinkjs');

module.exports = class extends BaseRest {
  constructor(ctx) {
    super(ctx);

    this.todosService = think.service('todos');
  }

  /**
   * 创建一条新的待办事项
   */
  async postAction() {
    const { todo } = this.ctx.request.body.post;
    const { userId } = { userId: 11 };
    const insterId = await this.todosService.create(userId, todo);
    this.body = {
      _id: insterId
    };
  }

  /**
   * 修改一条待办事项
   */
  putAction() {
    // 待办事项的唯一标识
    const todoId = this.get('id');
    // 获取新的待办事项内容
    const { complete, todo } = this.ctx.request.body.post;

    // 修改待办事项
    this.todosService.update(todoId, complete, todo);

    this.body = { todoId, complete, todo };
  }
};
